package com.cskaoyan.demo;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Demo1 {
    private static User user = new User();
    static {
        user.setUsername("李雷");
        user.setPassword("123456");
        user.setAge(25);
        user.setHobbies(new String[]{"唱","跳","RAP","篮球"});
        List<House> houses = new ArrayList<>();
        houses.add(new House("软件新城C13二楼", 1_000_000.0));
        houses.add(new House("软件新城C13四楼", 1_250_000.0));
        user.setHouseList(houses);
        user.setBirthday(new Date());
    }

    public static void main(String[] args) throws JsonProcessingException {
        // 将user实例转换为JSON字符串
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        String userJsonString = objectMapper.writeValueAsString(user);
        System.out.println("JSON：" + userJsonString);
        // 将JSON字符串转换为user实例
        User user1 = objectMapper.readValue(userJsonString, User.class);
        System.out.println("user1 = " + user1);


    }
}
